$(function(){
	// 选择星星
	$(".comment--xing").on("click", 'i', function(){
		var _this = $(this),
			_length = _this.attr("index") * 1;
		$(".comment--xing i").removeClass("on");
		for (var i = 0; i <= _length; i++) {
			$(".comment--xing .i" + i).addClass("on");
		}
	});

	// 提交评价按钮操作
	$("#comment-btn").on("click", function(){
		var startNum = $(".comment--xing i.on").length;
		var remark = $(".clean-remark").val();
		if(startNum==0){
			alert("请输入选择星级");
			return false;
		}
		if(remark == ""){
			alert("请输入评论内容");
			return false;
		}
		var num=$(".num").text();
		if(parseInt(num)>200){
			alert("输入内容不能超过200个字");
			return false;
		}
		alert($("#content").val());
		$.ajax({
			url : "../serviceComment/insertServiceComment.do",
			data : {
				"orderId": orderId,
				"content" : JSON.stringify($("#content").val()),
				"level" : startNum
			},
			type : "post",
			dataType : 'json',
			cache : false,
			success : function(ret){
				if(ret.success){
					alert("提交成功");
					window.location.href = "../jsp/service/serviceDetail.html?serviceId="+ret.serviceId
				}
			}
		})
	});

	// 实时监控 Textarea 字数
	// 实时监控 Textarea 字数
	  $("#content").on('input propertychange', function(event) {
	    var length = $(this).val().length;
	    if(length < 200){
	      $(".num").text(length);
	    }else{
	      //当第140个的时候提示，不能再输入了
	      $(".num").text(length);
	      alert("评价内容不能超出200字");
	    }
	  });
	$("#content").blur(function(){
		$("#content").off('input propertychange');
	});
});
function utf16toEntities(str) {
    var patt=/[\ud800-\udbff][\udc00-\udfff]/g;
    // 检测utf16字符正则
    str = str.replace(patt, function(char){
        var H, L, code;
        if (char.length===2) {
            H = char.charCodeAt(0);
            // 取出高位
            L = char.charCodeAt(1);
            // 取出低位
            code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00;
            // 转换算法
            return "&#" + code + ";";
        } else {
            return char;
        }
    });
    return str;
}

var comment =({
	queryPackageDetail :  function (){
	    	$.ajax({
				url : "../service/queryPackageDetail.do",
				data : {
					"orderId": orderId,
				},
				type : "GET",
				dataType : 'json',
				cache : false,
				success : function(ret){
					console.log(ret);
					if(ret.result = true){
						var serviceCount;
						if(ret.packageList && ret.packageList.length>0){
							$.each(ret.packageList,function(i,n){
								$("#serviceName").text(n.serviceName+"("+ret.packageList[0].address+")");
								$("#hours").text(ret.order[0].hours+"小时");
								$("#serviceDate").text(ret.order[0].start_serviceDate);
								$("#endServiceDate").text(ret.packageList[0].end_serviceDate);
							})
						}
						var done = 0;
						
					}
				}
			})
	    },
	    formatDate :   function (a) { 
	    	var now= new Date(a);   
	    	var year=now.getFullYear(); 
	    	var month=now.getMonth()+1; 
	    	var date=now.getDate(); 
	    	var hour=now.getHours(); 
	    	var minute=now.getMinutes(); 
	    	var second=now.getSeconds(); 
	    	return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;   
	    	} 
});
